<?php

namespace app\batch;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\models\{IntergralOrder, User as Users, Payment, Wechat,Douyin};
use think\Db;
use EasyWeChat\Factory;
use app\facade\Redis;


class Iorder extends Command
{
    protected $open = false;
    protected function configure()
    {
        $this->setName('Iorder')->setDescription('积分商城订单自动取消和完成');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->doCron();
        die();
    }

    protected function doCron()
    {
        $cmd = "ps aux | grep '/www/server/php/72/bin/php /www/wwwroot/garbage/think Message'|grep -v 'grep'| awk '{print $2}'";
        exec($cmd, $ps, $ret);
        if (count($ps) > 1) {
            die("batch of  is running");
        }
        ini_set('max_execution_time', 0);
        //10分钟未支付自动取消
        $time = date('Y-m-d H:i:s', time() - 10 * 60);
        (new IntergralOrder())->where(['status' => 0])->where('created', '<=', $time)->chunk(5, function ($list) {
            foreach ($list as $order) {
                $user = (new Users())->where('id', $order['user_id'])->find();
                Db::startTrans();
                try {
                    (new IntergralOrder())->where(['id' => $order['id'], 'status' => $order['status']])->update(['status' => 4]);
                    if ($order['total'] > 0) {
                        (new Users())->IntergralEdit($user['id'], $user['integral'], $order['total'], 3, $order['id'], '兑换商品取消');
                    }
                    Db::commit();
                } catch (\Exception $e) {
                    dump($e->getMessage());
                    Db::rollback();
                    echo '取消失败';
                }
                echo '取消成功';
            }
        });
        //超过15天订单自动确认
        $times = date('Y-m-d H:i:s', time() - 86400 * 15);
        (new IntergralOrder())->where(['status' => 2])->where('created', '<=', $times)->chunk(5, function ($list) {
            foreach ($list as $order) {
                Db::startTrans();
                try {
                    (new IntergralOrder())->where(['id' => $order['id'], 'status' => $order['status']])->update(['status' => 3]);
                    Db::commit();
                } catch (\Exception $e) {
                    dump($e->getMessage());
                    Db::rollback();
                    echo '确认失败';
                }
                echo '确认成功';
            }
        });
        //订单进行退款
        (new IntergralOrder())->where(['status' => 4, 'refund' => 2])->chunk(5, function ($list) {
            foreach ($list as $list) {
                $frtimes = Redis::get('orderRefunds' . $list['id']);
                if ($frtimes) {
                    continue;
                } else {
                    Redis::setex('orderRefunds' . $list['id'], 15, 15);
                }
                $payment = (new Payment())->where(['ordersn' => $list['id'], 'status' => 2, 'type' => 3])->find();
                if (empty($payment) || $list['payment'] == 0 || $payment['refund'] == 2) {
                    Db::startTrans();
                    try {
                        (new IntergralOrder())->where(['refund' => $list['refund'], 'id' => $list['id']])->update(['refund' => 3]);
                        Db::commit();
                    } catch (\Exception $e) {
                        Db::rollback();
                        echo $e->getMessage();
                        continue;
                    }
                } else {
                    switch ($payment['paytype']) {
                            //微信支付退款
                        case '1':
                            $refundInfo['refund_id'] = $payment['paymentid'];
                            $refundInfo['money'] = $payment['money'];
                            $paymentInfo['params'] = $payment['paydata'];
                            $paymentInfo['payment_id'] =  $payment['paymentid'];
                            $paymentInfo['money'] = $payment['money'];
                            (new Wechat())->refund($refundInfo, $paymentInfo);
                            break;
                        case '2':
                            $refundInfo['refund_id'] = $payment['paymentid'];
                            $refundInfo['money'] = $payment['money'];
                            $paymentInfo['params'] = $payment['paydata'];
                            $paymentInfo['payment_id'] =  $payment['paymentid'];
                            $paymentInfo['money'] = $payment['money'];
                            $res = (new Douyin())->refund($refundInfo, $paymentInfo);
                            dump($res);
                            break;
                        default:
                            # code...
                            break;
                    }
                }
            }
        });
    }
}
